package com.su.gulimall.search.thread;

import java.util.concurrent.*;

public class CombIneOne {
    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,200,10,
            TimeUnit.SECONDS,new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> {
            int sum=0;
            for (int j=0;j<100;j++)
            {
                sum+=j;
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务1结束:" + Thread.currentThread().getId());
            System.out.println(sum);
            return sum+"";
        }, executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2开始:" + Thread.currentThread().getId());
            return "hello";
        }, executor);
/*        //runAfterEitherAsync，有一个任务结束就执行任务3，不能感知结果,没有返回值
        future01.runAfterEitherAsync(future02,()->{
          System.out.println("任务3等待一个任务完成就执行开始");
        },executor);*/
        //acceptEitherAsync,有一个任务结束就执行任务3，能够感知任务1,2的结果，任务1,2，必须统一返回值，自己没有返回值
  /*     future01.acceptEitherAsync(future02,(res)->{
           System.out.println(res);
       },executor);*/
        CompletableFuture<String> future03 = future01.applyToEitherAsync(future02, (res) -> {
            return res+"haha";
        }, executor);
        System.out.println(future03.get());
    }
}
